const { getList, updateBlog } = require("../src/controller/blog");
const { SuccessModel } = require("../src/model/responseModel");

const handleBlogRoute = (req, res) => {
  const method = req.method;
  const id = req.query.id;
  const blogData = req.body;
  
  if(method === 'GET' && req.path === '/api/blog/list'){
    console.log('req.query', req.query)

    const author = req.query.author || '';
    const keyword = req.query.keyword || '';
    const listData = getList(author, keyword); // 根据参数获取数据
    return new SuccessModel(listData);
    // return {
    //   message: '获取博客列表的接口'
    // }
  }
  if(method === 'POST' && req.path === '/api/blog/new'){
    return {
      message: '新建博客的接口'
    }
  }
  if(method === 'POST' && req.path === '/api/blog/update'){
    const updateBlogData = updateBlog(blogData)
    if(updateBlogData){
      return new SuccessModel('更新博客成功')
    }
  }
  if(method === 'POST' && req.path === '/api/blog/delete'){
    return {
      message: '删除博客的接口'
    }
  }
}

module.exports = handleBlogRoute;